package cn.tedu.boot04.controller;

import cn.tedu.boot04.entity.Product;
import cn.tedu.boot04.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    ProductMapper mapper;
    @RequestMapping("/insert")
    public String insert(Product product){
        mapper.insert(product);
        return "添加完成!<a href='/'>返回首页</a>";
    }

    @RequestMapping("/select")
    public String select(){
        List<Product> list= mapper.select();
        String html="<table border=1>";
        html+="<caption>商品列表</caption>";
        html+="<tr><td>id</td><td>标题</td><td>价格</td><td>库存</td></tr>";
        for(Product product:list){
            html+="<tr>";
            html+="<td>"+product.getId()+"</td>";
            html+="<td>"+product.getTitle()+"</td>";
            html+="<td>"+product.getPrice()+"</td>";
            html+="<td>"+product.getNum()+"</td>";
            html+="<td><a href='/delete?id="+product.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/update")
    public String update(Product product){
        mapper.update(product);
        return "修改完成!<a href='/'>返回首页</a>";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        mapper.delete(id);
        return "删除完成!<a href='/select'>返回首页</a>";
    }

}
